<?php

namespace maotool\log;

use maotool\object\JsonUtil;
use maotool\util\ArrayUtil;
use maotool\util\DateUtil;

class Console
{

    public static $lines = [];

    public static function log(...$ages)
    {
        static::$lines[] = [
            "type" => "log",
            "ages" => $ages,
        ];
        self::terminal($ages);
    }

    public static function debug(...$ages)
    {
        static::$lines[] = [
            "type" => "debug",
            "ages" => $ages,
        ];
        self::terminal($ages, "34m");
    }

    public static function info(...$ages)
    {
        static::$lines[] = [
            "type" => "info",
            "ages" => $ages,
        ];
        self::terminal($ages);
    }

    public static function error(...$ages)
    {
        static::$lines[] = [
            "type" => "error",
            "ages" => $ages,
        ];
        self::terminal($ages, "31m");
    }

    public static function warn(...$ages)
    {
        static::$lines[] = [
            "type" => "warn",
            "ages" => $ages,
        ];
        self::terminal($ages, "33m");
    }

    private static function terminal($ages, $color = "37m")
    {
        $message = [];
        foreach ($ages as $age) {
            if (is_array($age) || is_object($age)) {
                $message[] = JsonUtil::encode($age);
            } else {
                $message[] = $age;
            }
        }
        file_put_contents('php://stderr', "\033[" . $color . "[" . DateUtil::now() . "] " . ArrayUtil::join($message, " ") . "\033[0m\n");
    }

}